<template>
    <!-- 基于 Element UI 新增和修改弹窗 -->
    <el-dialog :title="!dataForm.id ? '添加' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
        <!-- 新增和创建表单表单 -->
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()"
            label-width="100px">
            <el-form-item label="活动名称" prop="activityName">
                <el-input v-model="dataForm.activityName" placeholder="活动名称"></el-input>
            </el-form-item>
            <el-form-item label="开始时间" prop="startTime">
                <el-date-picker v-model="dataForm.startTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
                <el-date-picker v-model="dataForm.endTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="捐赠积分数" prop="baseDonateIntegral">
                <el-input-number v-model="dataForm.baseDonateIntegral" :min="0.01" placeholder="捐赠积分数"></el-input-number>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import * as api from '@/api/benefitActivity.js'
export default {
    data() {
        return {
            visible: false,
            dataForm: {
                id: 0,
                activityName: '',
                startTime: '',
                endTime: '',
                baseDonateIntegral: '',
            },
            dataRule: {
                activityName: [
                    { required: true, message: '活动名称为必填项', trigger: 'blur' }
                ],
                startTime: [
                    { required: true, message: '开始时间为必填项', trigger: 'blur' }
                ],
                endTime: [
                    { required: true, message: '结束时间为必填项', trigger: 'blur' }
                ],
                baseDonateIntegral: [
                    { required: true, message: '捐赠积分数为必填项', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        init(id) { // 初始化表单验证规则
            this.dataForm.id = id || 0
            this.visible = true
            this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                if (this.dataForm.id) {
                    api.benefitActivityDetailApi(id).then(res => {
                        this.dataForm = res;
                    })
                }
            })
        },
        // 表单数据提交
        dataSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    api.benefitActivityCreateApi(this.dataForm).then(res => {
                        // TODO 保存数据
                        this.$emit('refreshDataList', "form")
                    });
                }
            })
        }
    }
}
</script>